//
//  WYTCityPageVC.m
//  WuyouTang
//
//  Created by yunbao02 on 2024/8/28.
//  Copyright © 2024 cat. All rights reserved.
//

#import "WYTCityPageVC.h"

#import <TYTabPagerBar.h>
#import <TYPagerController.h>
#import "WYTCityItemVC.h"
#import "searchVC.h"
#import "YBCitySelVC.h"

@interface WYTCityPageVC ()<TYTabPagerBarDelegate,TYTabPagerBarDataSource,TYPagerControllerDelegate,TYPagerControllerDataSource>{
    UIImageView *mask_top;

    NSString *_currentCity;
}
@property(nonatomic,strong)NSArray *pageRawArray;

@property(nonatomic,strong)TYTabPagerBar *tabBar;
@property(nonatomic,strong)TYPagerController *pagerController;
@property(nonatomic,strong)NSArray *dataArray;

@property(nonatomic,strong)UIButton *search;
@property(nonatomic,strong)UIButton *liveBtn;

@end

@implementation WYTCityPageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.naviView.hidden = YES;
    _pageRawArray = [NSArray array];
    
    _currentCity = @"濮阳市";
    YBWeakSelf;
    [[RKLBSManager shareManager] startLocation];
    [[RKLBSManager shareManager] locationComplete:^(BOOL isSuc, NSArray<CLPlacemark *> *placemarks, NSArray<NSString *> *pcaArray) {
        _currentCity = @"濮阳";
        [weakSelf infoSet:NO];
        /*
        if ([PublicObj checkNull:_currentCity]) {
            _currentCity = [PublicObj checkNull:minstr(pcaArray[1])]?@"":pcaArray[1];
            if ([_currentCity hasSuffix:@"市"]) {
                _currentCity = [_currentCity substringToIndex:_currentCity.length-1];
            }
            [weakSelf infoSet:NO];
        }
        */
    }];
    
    TYTabPagerBar *tabBar = [[TYTabPagerBar alloc]init];
    tabBar.dataSource = self;
    tabBar.delegate = self;
    tabBar.layout.barStyle = TYPagerBarStyleProgressView;
    tabBar.layout.selectedTextColor = UIColor.whiteColor;
    tabBar.layout.normalTextColor = UIColor.whiteColor;
    tabBar.layout.selectedTextFont = [UIFont boldSystemFontOfSize:18];
    tabBar.layout.normalTextFont = [UIFont boldSystemFontOfSize:18];
    tabBar.layout.progressColor = UIColor.whiteColor;
    tabBar.layout.progressHeight = 4;
    tabBar.layout.progressRadius = 2;
    tabBar.layout.progressHorEdging = 10;
    tabBar.layout.progressVerEdging = 5;
    tabBar.layout.cellWidth = 0;
    tabBar.layout.cellSpacing = 0;
    tabBar.backgroundColor = UIColor.clearColor;
    
    [tabBar registerClass:[TYTabPagerBarCell class] forCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier]];
    [self.view addSubview:tabBar];
    _tabBar = tabBar;
    TYPagerController *pagerController = [[TYPagerController alloc] init];
    pagerController.dataSource = self;
    pagerController.delegate = self;
    pagerController.layout.adjustScrollViewInset = NO;
    pagerController.layout.scrollView.bounces = NO;
    pagerController.view.backgroundColor = UIColor.clearColor;
    [self addChildViewController:pagerController];
    [self.view addSubview:pagerController.view];
    _pagerController = pagerController;
    

    mask_top = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _window_width, 100+statusbarHeight)];
    [mask_top setImage:[UIImage imageNamed:@"home_mask_top"]];
    [self.view addSubview:mask_top];
    
    UIImageView* mask_buttom = [[UIImageView alloc] initWithFrame:CGRectMake(0,  _window_height-49-ShowDiff-44, _window_width, 49)];
    [mask_buttom setImage:[UIImage imageNamed:@"home_mask_bottom"]];
    mask_buttom.userInteractionEnabled = YES;
    [self.view addSubview:mask_buttom];
    
    [self.view bringSubviewToFront:tabBar];
    [self.view addSubview:self.search];
    [self.view addSubview:self.liveBtn];
    //[self infoSet:NO];
   
    //rk_net
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(needRefreshPage) name:@"FirstGetVideoClass" object:nil];
   
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(needRefreshPage) name:@"topTabbarScrolUnEnable" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(needRefreshPage) name:@"topTabbarScrolEnable" object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doubleClickChangeCity) name:CityDoubleClick object:nil];
}
-(void)doubleClickChangeCity {
    NSInteger curIndx = _tabBar.curIndex;
    YBCitySelVC *selVC = [[YBCitySelVC alloc]init];
    selVC.citySelEvent = ^(NSString *cityName) {
        _currentCity = cityName;
        [PublicObj updataNewCity:cityName];
        [[NSNotificationCenter defaultCenter] postNotificationName:wytChangeLocCity object:cityName];
        [_pagerController reloadData];
        [_pagerController scrollToControllerAtIndex:curIndx animate:NO];
    };
    [[YBBaseAppDelegate sharedAppDelegate]pushViewController:selVC animated:YES];
}
-(void)topTabbarScrolUnEnable{
    _tabBar.userInteractionEnabled =NO;
    _pagerController.scrollView.scrollEnabled = NO;
}
-(void)topTabbarScrolEnable{
    _tabBar.userInteractionEnabled =YES;
    _pagerController.scrollView.scrollEnabled = YES;

}
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    _tabBar.frame = CGRectMake(50, statusbarHeight+20, _window_width-105, 44);
    _pagerController.view.frame = CGRectMake(0, 0, _window_width, _window_height-49-ShowDiff);
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

//rk_net
-(void)needRefreshPage {
    [self infoSet:YES];
}
//rk_net
-(void)infoSet:(BOOL)refresh {
    
    [WYNetworking postWithUrl:@"Home.getCityClassLists" Dic:nil Suc:^(int code, id info, NSString *msg) {
        if (code == 0) {
            if ([PublicObj isArray:info]) {
                [self dealPageData:[NSArray arrayWithArray:info]];
            }else {
                [self dealPageData:@[]];
            }
        }else {
            [MBProgressHUD showPop:msg];
            [self dealPageData:@[]];
        }
    } Fail:^(id fail) {
        [self dealPageData:@[]];
    }];
}
-(void)dealPageData:(NSArray *)infoA {
    NSDictionary *followDic = @{
        @"id":@"-1",
        @"title":@"关注",
    };
    NSMutableArray *all_m_a = [NSMutableArray array];
    [all_m_a addObject:followDic];
    [all_m_a addObjectsFromArray:infoA];
    
    _pageRawArray = [NSArray arrayWithArray:all_m_a];
    
    NSMutableArray *m_array = [NSMutableArray array];
    for (NSDictionary *classDic in _pageRawArray) {
        NSString *title = minstr([classDic valueForKey:@"title"]);
        [m_array addObject:title];
    }
    _dataArray = [NSArray arrayWithArray:m_array];
    [_tabBar reloadData];
    [_pagerController reloadData];
    
    [_pagerController scrollToControllerAtIndex:1 animate:YES];
}
-(UIButton *)search {
    if (!_search) {
        _search = [UIButton buttonWithType:0];
        [_search setImage:[UIImage imageNamed:@"wyt_search"] forState:0];
        _search.frame = CGRectMake(10, 20+statusbarHeight, 40, 40);
        [_search addTarget:self action:@selector(doSearchBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _search;
}
- (UIButton *)liveBtn {
    if (!_liveBtn) {
        _liveBtn = [UIButton buttonWithType:0];
        [_liveBtn setImage:[UIImage imageNamed:@"wyt_change"] forState:0];
        _liveBtn.frame = CGRectMake(_window_width-50, 20+statusbarHeight, 40, 40);
        [_liveBtn addTarget:self action:@selector(doLiveBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _liveBtn;
}
#pragma mark - 点击事件
-(void)doSearchBtn {
    searchVC *search = [[searchVC alloc]init];
    search.searchFrom = SearchFrom_City;
    search.curCity = _currentCity;
    [[YBBaseAppDelegate sharedAppDelegate]pushViewController:search animated:YES];
}
-(void)doLiveBtn {
    [PublicObj changeListState];
    [[NSNotificationCenter defaultCenter] postNotificationName:wytChangeCityUi object:nil];
}
#pragma mark - TYTabPagerBarDataSource
- (NSInteger)numberOfItemsInPagerTabBar {
    return _dataArray.count;
}
- (UICollectionViewCell<TYTabPagerBarCellProtocol> *)pagerTabBar:(TYTabPagerBar *)pagerTabBar cellForItemAtIndex:(NSInteger)index {
    UICollectionViewCell<TYTabPagerBarCellProtocol> *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier] forIndex:index];
    cell.titleLabel.text = _dataArray[index];
    return cell;
}

#pragma mark - TYTabPagerBarDelegate
- (CGFloat)pagerTabBar:(TYTabPagerBar *)pagerTabBar widthForItemAtIndex:(NSInteger)index {
    NSString *title = _dataArray[index];
    return [pagerTabBar cellWidthForTitle:title];
}
- (void)pagerTabBar:(TYTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index {
    [_pagerController scrollToControllerAtIndex:index animate:YES];
}

#pragma mark - TYPagerControllerDataSource
- (NSInteger)numberOfControllersInPagerController {
    return _dataArray.count;
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index prefetching:(BOOL)prefetching {
    NSDictionary *subDic = _pageRawArray[index];
    WYTCityItemVC *itemVC = [[WYTCityItemVC alloc]init];
    itemVC.curCity = _currentCity;
    itemVC.classID = strFormat([subDic valueForKey:@"id"]);
    return itemVC;
}
#pragma mark - TYPagerControllerDelegate
- (void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex animated:(BOOL)animated {
    [_tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex animate:animated];
}

-(void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress {
    [_tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex progress:progress];
}




@end
